package com.hotel.controller;

import com.hotel.entity.Room;
import com.hotel.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
// 【重要修改】将公共前缀 /api/admin/rooms 放在这里，这样所有方法都会以这个路径开头
@RequestMapping("/api/admin/rooms")
public class RoomController {
    @Autowired
    private RoomService roomservice;

    // POST /api/admin/rooms
    @PostMapping
    public ResponseEntity<Room> addRoom(@RequestBody Room room) {
        roomservice.save(room);
        return new ResponseEntity<>(room, HttpStatus.CREATED);
    }

    // GET /api/admin/rooms
    @GetMapping
    public List<Room> findAll() {
        return roomservice.findAll();
    }

    // GET /api/admin/rooms/available
    @GetMapping("/available")
    List<Room> findAvailableRooms() {
        return roomservice.findAvailableRooms();
    }

    // DELETE /api/admin/rooms/{id}
    // 【重要修改】修正拼写：roooms -> rooms，并接收 Long ID
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> delete(@PathVariable Long id) { // 接收 Long 类型的 ID
        try {
            // 调用 Service 层中我们之前定义的 deleteRoom(Long id) 方法
            roomservice.deleteRoom(id);
            return new ResponseEntity<>(HttpStatus.NO_CONTENT); // 204 No Content
        } catch (RuntimeException e) {
            if (e.getMessage().contains("Room not found")) {
                return new ResponseEntity<>(HttpStatus.NOT_FOUND); // 404
            } else if (e.getMessage().contains("occupied")) {
                return new ResponseEntity<>(HttpStatus.CONFLICT); // 409
            }
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); // 500
        }
    }

    // PUT /api/admin/rooms/{id}
    // 【重要修改】@RequestMapping("/api/admin/rooms") 已经放在类级别，所以这里只需 /{id}
    @PutMapping("/{id}")
    public ResponseEntity<Room> updateRoom(@PathVariable Long id, @RequestBody Room updatedRoom) {
        try {
            Room room = roomservice.updateRoom(id, updatedRoom); // 通过注入的实例调用
            return new ResponseEntity<>(room, HttpStatus.OK);
        } catch (RuntimeException e) {
            if (e.getMessage().contains("Room not found")) {
                return new ResponseEntity<>(HttpStatus.NOT_FOUND);
            }
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    //批量导入房间，多条数据创建
    // POST /api/admin/rooms/import
    @PostMapping("/import")
    public ResponseEntity<Map<String, Object>> bulkImportRooms(@RequestBody List<Room> rooms) {
        int importedCount = 0;
        try {
            for (Room room : rooms) {
                roomservice.save(room);
                importedCount++;
            }
            Map<String, Object> response = new HashMap<>();
            response.put("message", "成功导入");
            response.put("importedCount", importedCount);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("message", "导入失败: " + e.getMessage());
            errorResponse.put("importedCount", importedCount);
            return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}